package com.keda;

import com.entity.User;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

public class test_piliang_add {
    public static void main(String[] args) throws IOException {

        //创建es 客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200))
        );

       //批量数据添加
        BulkRequest request = new BulkRequest();

        request.add(new IndexRequest().index("user").id("1002").source(XContentType.JSON,"name","lisi","age","20","sex","男"));
         request.add(new IndexRequest().index("user").id("1003").source(XContentType.JSON,"name","王五","age","20","sex","男"));
         request.add(new IndexRequest().index("user").id("1004").source(XContentType.JSON,"name","1111","age","50","sex","女"));

    request.add(new IndexRequest().index("user").id("1005").source(XContentType.JSON,"name","wangwu1","age","20","sex","男"));

    request.add(new IndexRequest().index("user").id("1006").source(XContentType.JSON,"name","wangwu2","age","30","sex","女"));

    request.add(new IndexRequest().index("user").id("1007").source(XContentType.JSON,"name","wangwu3","age","40","sex","男"));


        BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);

        //获取批量插入时间
        System.out.println(response.getTook());

        System.out.println(response.getItems());


        //关闭es客户端
        try {
            esClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
